/*
 * Copyright 2009 Original Author(s)
 * 
 * This file is part of Kommando
 * 
 * Kommando is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Kommando is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Kommando.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.kommando.tray.internal;

import java.awt.AWTException;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;

import org.kommando.core.Kommando;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.osgi.util.tracker.ServiceTracker;

/**
 * 
 * 
 * @author Peter De Bruycker
 */
public class SysTrayActivator implements BundleActivator {

	private SystemTray systemTray;
	private TrayIcon trayIcon;
	private ServiceTracker kommandoTracker;

	public void start(BundleContext context) throws Exception {
		systemTray = SystemTray.getSystemTray();

		kommandoTracker = new ServiceTracker(context, Kommando.class.getName(),
				null) {
			@Override
			public Object addingService(ServiceReference reference) {
				addSystemTrayIcon();

				return super.addingService(reference);
			}

			@Override
			public void removedService(ServiceReference reference,
					Object service) {
				removeSystemTrayIcon();

				super.removedService(reference, service);
			}
		};
		kommandoTracker.open();
	}

	private void addSystemTrayIcon() {
		Image image = new BufferedImage(16, 16, BufferedImage.TYPE_INT_RGB);
		Graphics2D graphics = (Graphics2D) image.getGraphics();
		graphics.setColor(Color.YELLOW);
		graphics.fillRect(0, 0, 16, 16);
		graphics.dispose();

		trayIcon = new TrayIcon(image);
		trayIcon.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if (e.getButton() == MouseEvent.BUTTON1
						&& e.getClickCount() == 1) {
					Kommando kommando = (Kommando) kommandoTracker.getService();
					kommando.summon();
				}
			}
		});

		PopupMenu popupMenu = new PopupMenu();
		MenuItem exit = new MenuItem("Exit");
		exit.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				Kommando kommando = (Kommando) kommandoTracker.getService();
				kommando.close();
			}
		});
		popupMenu.add(exit);

		trayIcon.setPopupMenu(popupMenu);

		try {
			systemTray.add(trayIcon);
		} catch (AWTException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	}

	private void removeSystemTrayIcon() {
		systemTray.remove(trayIcon);
		trayIcon = null;
		systemTray = null;
	}

	public void stop(BundleContext context) throws Exception {
		kommandoTracker.close();
	}

}
